<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.logging.*" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<jsp:useBean id="rs1" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="rs2" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="rs3" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="sysRemind" class="weaver.system.SysRemindWorkflow" scope="page"/>

<%
	
	Log log = LogFactory.getLog(this.getClass());
	String method = Util.null2String(request.getParameter("method"));	//方法类型
	if("MyInfo".equals(method)){
		//当前日期
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
		String currDate = sdf.format(new Date());
		String userid = Util.null2String(request.getParameter("userid"));	//用户id
		String conEndDate = Util.null2String(request.getParameter("conEndDate"));	//合同结束日期		
		String remake = "您好,员工"+GetName(userid)+"的合同将于"+conEndDate+"到期，请注意！";	//备注
		String Title = "合同到期提醒("+GetName(userid)+")-"+currDate;
		String resourceid = "";
		StringBuffer stBuffer = new StringBuffer();
		String seachRole = "select * from HrmRoleMembers where roleid =25 ";
		log.info(seachRole);
		rs1.execute(seachRole);
		while(rs1.next()){
			resourceid = rs1.getString("resourceid");
			stBuffer.append(resourceid+",");
		}
		resourceid = stBuffer.substring(0, stBuffer.length()-1);
		sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), resourceid, remake);
	}
	else if("Wqsq".equals(method)){
		log.info("外勤签到申请提醒开始");
		String userid = Util.null2String(request.getParameter("userid"));	//用户id
		String currentDate = Util.null2String(request.getParameter("currentDate"));	//日期
		String currentTime = Util.null2String(request.getParameter("currentTime"));	//时间
		String card = Util.null2String(request.getParameter("card"));
		String cardsj = Util.null2String(request.getParameter("cardsj"));
		String remake = Util.null2String(request.getParameter("remake"));	//备注
		String Title = "外勤打卡申请("+GetName(userid)+")"+currentDate+" "+currentTime;
		//查询用户所在部门的部门等级，部门ID,职级，部门负责人等信息
		String sql = "select dep.depgrade,dep.deptid,hrm.joblevel,dep.directorI,dep.directorII,dep.directorIII from HrmDepartmentDefined as dep left join HrmResource as hrm on dep.deptid=hrm.departmentid where hrm.id=?";
		rs1.executeQuery(sql,userid);
		String depgrade = "";
		String deptid = "";
		String joblevel = "";
		String directorI = "";
		String directorII = "";
		String directorIII = "";
		String depManager = "";
		while(rs1.next()){
			depgrade = rs1.getString("depgrade");
			deptid = rs1.getString("deptid");
			joblevel = rs1.getString("joblevel");
			directorI = rs1.getString("directorI");
			directorII = rs1.getString("directorII");
			directorIII = rs1.getString("directorIII");
			
			String sql2 = "select id from hrmresource where id=CONCAT('',(select (case depgrade when 3 then  directorIII when 2  then directorII when 1 then directorII end)"; 
			   sql2 += " as bmfzr from HrmDepartmentDefined where  depgrade="+depgrade+" and deptid="+deptid+" ))";
			rs2.execute(sql2);
			if(rs2.next()){
				depManager = rs2.getString("id");
			}
		}
		
		log.info("用户："+GetName(userid)+" 部门ID："+deptid+" 部门等级："+depgrade+" 职级："+joblevel);
		log.info("该部门矩阵负责人："+" 分管领导："+directorI+" 二级部门负责人："+directorII+" 三级部门负责人："+directorIII);
		boolean flag=false;
		Boolean result = CompareIsDepManager(userid,depManager);
		log.info("result="+result);
		String requestid="";
		//如果是部门负责人
		if(result){
			if("3".equals(depgrade)){	//发送给二级部门负责人
				try {
					requestid = sendWorkFlow(Title,userid,directorII,remake);
					log.info("=====requestid:"+requestid);
					//sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorII, remake);
					flag=true;
				} catch (Exception e1) {
					flag=false;
					e1.printStackTrace();
				}
			}
			else if("2".equals(depgrade)){	//发送给二级部门考勤员
				try {
					String sql3 = "select kqzy from Matrixtable_2 where id= "+deptid;
					rs3.execute(sql3);
					if(rs3.next()){
						String kqzy = rs3.getString("kqzy");
						requestid = sendWorkFlow(Title,userid,kqzy,remake);
						log.info("=====requestid:"+requestid);
						//sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), kqzy, remake);
						flag=true;
					}
					
				} catch (Exception e1) {
					flag=false;
					e1.printStackTrace();
				}
			}
		}
		//不是部门负责人
		else{
			if("3".equals(depgrade)){	//发送给三级部门负责人
				try {
					requestid = sendWorkFlow(Title,userid,directorIII,remake);
					log.info("=====requestid:"+requestid);
					//sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorIII, remake);
					flag=true;
				} catch (Exception e1) {
					flag=false;
					e1.printStackTrace();
				}
			}
			else if("2".equals(depgrade)){	//发送给二级部门负责人
				try {
					requestid = sendWorkFlow(Title,userid,directorII,remake);
					log.info("=====requestid:"+requestid);
					//sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorII, remake);
					flag=true;
				} catch (Exception e1) {
					flag=false;
					e1.printStackTrace();
				}
			}
		}
		
		
		/**
		try {
			sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), depManager, remake);
		} catch (Exception e1) {	
			e1.printStackTrace();
		}
		**/
		RecordSet rs=new RecordSet();
		String sqlstr="insert into uf_wqsq(sqrid,sqrq,sqcard,cardsj,sqdatetime,reason)values(?,?,?,?,?,?)";
		boolean inflag=rs.executeUpdate(sqlstr, userid,currentDate,card,cardsj,currentDate+" "+currentTime,remake);
		if(inflag&&flag){
			out.print("1");
		}else{
			out.print("0");
		}
		
		log.info("外勤签到申请提醒结束");
		
	}
%>

<%!
	/**
		获取用户名
	**/
	public static String GetName(String userid){
		
		RecordSet rs = new RecordSet();
		String sql = "select  lastname from HrmResource where id="+userid;
		rs.execute(sql);
		String Name = "";
		if(rs.next()){
			Name = rs.getString("lastname");
		}
		return Name;
	}
	/**
		判断当前用户是否是部门负责人
		返回true代表是
	**/
	public static Boolean CompareIsDepManager(String userid,String depManager){
		Boolean flag = true;
		if(Util.getIntValue(userid)!=Util.getIntValue(depManager)){
			flag = false;
		}
		else{
			flag = true;
		}
		return flag;
	}

	public static String sendWorkFlow(String Title,String Creater,String Users,String Content){
		String requestid="";
		try{
			//流程信息缓存类
			RequestInfo ri = new RequestInfo();
				
			//创建人id
			ri.setCreatorid(Creater);
			//流程id
			ri.setWorkflowid("59");
			//提醒    默认0
			ri.setRemindtype("0");
			//紧急程度
			ri.setRequestlevel("1");
			//标题
			ri.setDescription(Title);
			//流程是否自动流转到下一个节点
			ri.setIsNextFlow("1");

			//添加主表数据
			MainTableInfo mainInfo = new MainTableInfo();
			//添加主表字段属性
			Property[] p = new Property[3];
			p[0] = new Property();
			p[0].setName("resource_n");
			p[0].setValue(Creater);    //执行者
			
			p[1] = new Property();
			p[1].setName("mutiresource");	//提醒者
			p[1].setValue(Users);		
			
			p[2] = new Property(); //备注
			p[2].setName("remark");
			p[2].setValue(Content);	

			//将以上信息添加到表单中
			mainInfo.setProperty(p);
			ri.setMainTableInfo(mainInfo);
			//流程服务类
			RequestService service = new RequestService();
			//发送流程 
			requestid=service.createRequest(ri);	
		}catch(Exception e){
			e.printStackTrace();
		}
		return requestid;
	}
%>
